
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/modules/matrices/immutablematrices.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:26:19 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Immutable Matrices &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
    <script src="../../_static/jquery.js"></script>
    <script src="../../_static/underscore.js"></script>
    <script src="../../_static/doctools.js"></script>
    <script src="../../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../../live.sympy.org/static/live-sphinx.js"></script>
    
    <link rel="shortcut icon" href="../../_static/sympy-notailtext-favicon.ico"/>
    <link href="immutablematrices.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Matrix Expressions" href="expressions.html" />
    <link rel="prev" title="Sparse Tools" href="sparsetools.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="expressions.html" title="Matrix Expressions"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="sparsetools.html" title="Sparse Tools"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="index.html" accesskey="U">Matrices</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Immutable Matrices</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="immutable-matrices">
<h1>Immutable Matrices<a class="headerlink" href="#immutable-matrices" title="Permalink to this headline">¶</a></h1>
<p>The standard <a class="reference internal" href="dense.html#sympy.matrices.dense.Matrix" title="sympy.matrices.dense.Matrix"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Matrix</span></code></a> class in SymPy is mutable. This is important for
performance reasons but means that standard matrices cannot interact well with
the rest of SymPy. This is because the <a class="reference internal" href="../core.html#sympy.core.basic.Basic" title="sympy.core.basic.Basic"><code class="xref py py-class docutils literal notranslate"><span class="pre">Basic</span></code></a> object, from which most
SymPy classes inherit, is immutable.</p>
<p>The mission of the <a class="reference internal" href="#sympy.matrices.immutable.ImmutableDenseMatrix" title="sympy.matrices.immutable.ImmutableDenseMatrix"><code class="xref py py-class docutils literal notranslate"><span class="pre">ImmutableDenseMatrix</span></code></a> class, which is aliased as
<a class="reference internal" href="#sympy.matrices.immutable.ImmutableMatrix" title="sympy.matrices.immutable.ImmutableMatrix"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ImmutableMatrix</span></code></a> for short, is to bridge the tension
between performance/mutability and safety/immutability. Immutable matrices can
do almost everything that normal matrices can do but they inherit from
<a class="reference internal" href="../core.html#sympy.core.basic.Basic" title="sympy.core.basic.Basic"><code class="xref py py-class docutils literal notranslate"><span class="pre">Basic</span></code></a> and can thus interact more naturally with the rest of SymPy.
<a class="reference internal" href="#sympy.matrices.immutable.ImmutableMatrix" title="sympy.matrices.immutable.ImmutableMatrix"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ImmutableMatrix</span></code></a> also inherits from <a class="reference internal" href="expressions.html#sympy.matrices.expressions.MatrixExpr" title="sympy.matrices.expressions.MatrixExpr"><code class="xref py py-class docutils literal notranslate"><span class="pre">MatrixExpr</span></code></a>, allowing it to
interact freely with SymPy’s Matrix Expression module.</p>
<p>You can turn any Matrix-like object into an <a class="reference internal" href="#sympy.matrices.immutable.ImmutableMatrix" title="sympy.matrices.immutable.ImmutableMatrix"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ImmutableMatrix</span></code></a> by calling
the constructor</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Matrix</span><span class="p">,</span> <span class="n">ImmutableMatrix</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">],</span> <span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">IM</span> <span class="o">=</span> <span class="n">ImmutableMatrix</span><span class="p">(</span><span class="n">M</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">IM</span>
<span class="go">Matrix([</span>
<span class="go">[1, 2, 3],</span>
<span class="go">[4, 0, 6],</span>
<span class="go">[7, 8, 9]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">IM</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">5</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">Can not set values in Immutable Matrix. Use Matrix instead.</span>
</pre></div>
</div>
<section id="module-sympy.matrices.immutable">
<span id="immutablematrix-class-reference"></span><h2>ImmutableMatrix Class Reference<a class="headerlink" href="#module-sympy.matrices.immutable" title="Permalink to this headline">¶</a></h2>
<dl class="py attribute">
<dt class="sig sig-object py" id="sympy.matrices.immutable.ImmutableMatrix">
<span class="sig-prename descclassname"><span class="pre">sympy.matrices.immutable.</span></span><span class="sig-name descname"><span class="pre">ImmutableMatrix</span></span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/matrices/immutable.py#L80-L132"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.matrices.immutable.ImmutableMatrix" title="Permalink to this definition">¶</a></dt>
<dd><p>alias of <a class="reference internal" href="#sympy.matrices.immutable.ImmutableDenseMatrix" title="sympy.matrices.immutable.ImmutableDenseMatrix"><code class="xref py py-class docutils literal notranslate"><span class="pre">sympy.matrices.immutable.ImmutableDenseMatrix</span></code></a></p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.matrices.immutable.ImmutableDenseMatrix">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.matrices.immutable.</span></span><span class="sig-name descname"><span class="pre">ImmutableDenseMatrix</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/matrices/immutable.py#L80-L132"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.matrices.immutable.ImmutableDenseMatrix" title="Permalink to this definition">¶</a></dt>
<dd><p>Create an immutable version of a matrix.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">eye</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.matrices</span> <span class="kn">import</span> <span class="n">ImmutableMatrix</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ImmutableMatrix</span><span class="p">(</span><span class="n">eye</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>
<span class="go">Matrix([</span>
<span class="go">[1, 0, 0],</span>
<span class="go">[0, 1, 0],</span>
<span class="go">[0, 0, 1]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">_</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">42</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">Cannot set values of ImmutableDenseMatrix</span>
</pre></div>
</div>
</dd></dl>

</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Immutable Matrices</a><ul>
<li><a class="reference internal" href="#module-sympy.matrices.immutable">ImmutableMatrix Class Reference</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="sparsetools.html"
                        title="previous chapter">Sparse Tools</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="expressions.html"
                        title="next chapter">Matrix Expressions</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/modules/matrices/immutablematrices.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="expressions.html" title="Matrix Expressions"
             >next</a> |</li>
        <li class="right" >
          <a href="sparsetools.html" title="Sparse Tools"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="index.html" >Matrices</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Immutable Matrices</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/modules/matrices/immutablematrices.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:26:20 GMT -->
</html>